package org.easy.aliintersms;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({InterSmsProperties.class})
@AutoConfigureAfter
@Slf4j
public class AliInterSmsAutoConfiguration implements InitializingBean {

    private final InterSmsProperties interSmsProperties;

    public AliInterSmsAutoConfiguration(InterSmsProperties interSmsProperties) {
        this.interSmsProperties = interSmsProperties;
    }

    @Bean
    @ConditionalOnMissingBean
    public AlibabaInterSmsTemplate alibabaInterSmsTemplate() {
        return new AlibabaInterSmsTemplate(interSmsProperties.getAppId(), interSmsProperties.getAppKey());
    }

    @Override
    public void afterPropertiesSet() {
        checkParam();
    }

    private void checkParam() {
        if (interSmsProperties.getAppId() == null ||
                interSmsProperties.getAppKey() == null
        ) {
            log.error("未配置" + InterSmsProperties.class.getTypeName());
        }

    }
}
